import {Notify} from "vant";
import request from "@/utils/request";
import {reactive, ref} from "vue";
import {Local} from "@/utils/storage";
import router from "@/router";

const userInfo = Local.get('userInfo')

const modifyForm = reactive({
    id: 1,
    phone: "",
    password: "",
    iconUrl: ""
})

const teacherForm = reactive({
    teacherId: '',
    gender: 1,
    teacherName: "",
    subject: ""
});

const studentForm = reactive({
    studentId: 1,
    gender: 2,
    studentName: ""
});

const parentForm = reactive({
    parentId: '',
    gender: 2,
    parentName: ""
});
export function perfect(perfectInfo) {
    console.log(perfectInfo);
    if(userInfo.role === 1){
        teacherForm.teacherId = perfectInfo.id
        teacherForm.teacherName = perfectInfo.name
        teacherForm.gender = perfectInfo.gender
        teacherForm.subject = perfectInfo.subject
        request.post('perfect/teacher', teacherForm).then(res => {
            if (res.code === 2000) {
                Notify({ type: 'success', message: res.message });
                return res.data;
            }
            else {
                Notify({ type: 'danger', message: res.data });
                return ''
            }
        }).catch(error => {
            return error;
        });
    }
    else if(userInfo.role === 3){
        studentForm.studentId = perfectInfo.id
        studentForm.studentName = perfectInfo.name
        studentForm.gender = perfectInfo.gender
        request.post('perfect/student', studentForm).then(res => {
            if (res.code === 2000) {
                Notify({ type: 'success', message: res.message });
                return res.data;
            }
            else {
                Notify({ type: 'danger', message: res.data });
                return ''
            }
        }).catch(error => {
            return error;
        });
    }else{
        parentForm.parentId = perfectInfo.id
        parentForm.parentName = perfectInfo.name
        parentForm.gender = perfectInfo.gender
        request.post('perfect/parent', parentForm).then(res => {
            if (res.code === 2000) {
                Notify({ type: 'success', message: res.message });
                return res.data;
            }
            else {
                Notify({ type: 'danger', message: res.data });
                return ''
            }
        }).catch(error => {
            return error;
        });
    }

}

export function modify(modifyInfo,repassword,oldpassword){
    console.log(userInfo.token)
    if (modifyInfo.password !== repassword) {
        Notify({ type: 'danger', message: '两次密码不一致' });
        return;
    }
    // if (oldpassword !== userInfo.password) {
    //     Notify({ type: 'danger', message: '旧密码错误' });
    //     console.log(oldpassword)
    //     console.log(userInfo.password)
    //     return;
    // }
    console.log(modifyInfo);
    request.post('user/modify',modifyInfo).then(res => {
        if (res.code === 2000) {
            Notify({ type: 'success', message: res.message });
            Local.set('userInfo', res.data);
            router.push({ path: '/my' });
        } else {
            Notify({ type: 'danger', message: res.message });
        }
    }).catch(error => {
        return error;
    });
}